<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="用户昵称" prop="name">
      <el-input v-model="form.name" maxlength="30" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { handUser } from "@/api/index";
export default {
  props: {
    user: {
      type: Object,
    },
  },
  data() {
    return {
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    user: {
      handler(user) {
        if (user) {
          this.form = { name: user.name };
        }
      },
      immediate: true,
    },
  },
  methods: {
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          handUser({ id: this.user.id, ...this.form }).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.user.name = this.form.name;
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
  },
};
</script>
